package com.example.neusoftfood_828lhl.activity;

import androidx.annotation.Nullable;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.neusoftfood_828lhl.R;
import com.example.neusoftfood_828lhl.bean.LoginBean;
import com.example.neusoftfood_828lhl.listener.RetrofitListener;
import com.example.neusoftfood_828lhl.model.UserModel;

public class LoginActivity extends BaseActivity implements RetrofitListener<LoginBean> {

    private EditText etname,etpass;
    private Button btnlogin,btnRegister;
    String username="",password="",sessionID="";
    private Switch sw;
    private SharedPreferences sp;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }
    void initViews(){
        etname=findViewById(R.id.etname);
        etpass=findViewById(R.id.etpass);
        btnlogin= findViewById(R.id.btnlogin);
        btnRegister=(Button)findViewById(R.id.btnregister);
        sw=findViewById(R.id.switch1);

    }

    private void login(){
        UserModel userModel = new UserModel();
        userModel.userLogin(username,password,this);
    }
    @Override
    void initEvents() {
        btnlogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                username=etname.getText().toString();
                password=etpass.getText().toString();
                if (password.equals("")||etpass.equals("")){
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                }
                else{
                    login();
                }
            }
        });
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String et_user=data.getStringExtra("user");
        etname.setText(et_user);
    }

    @Override
    void initData() {

    }

    private void saveSP(){
        boolean remember =sw.isChecked();
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",remember);
        editor.putString("sessionID",sessionID);
        boolean b = editor.commit();
        Log.i("LoginActivity","---写入SP返回值"+b);
    }

    @Override
    public void onSuccess(LoginBean loginBean, int flag) {
        if(!loginBean.getUserid().equals("0")) {
            saveSP();
            showToast( "successful");
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
        else
            showToast("error");
    }

    @Override
    public void onFail() {
        showToast("lost");
    }
    private void readSP(){
        String name=sp.getString("username","");
        String pass=sp.getString("password","");
        boolean b=sp.getBoolean("remember",false);
        sw.setChecked(b);
        if(b){
            etname.setText(name);
            etpass.setText(pass);
        }
    }
    public void saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username", etname.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", etpass.getText().toString());
        editor.commit();//提交修改
    }

}
